// 平滑滚动效果
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});

// 页面加载时的简单动画
window.addEventListener('load', function() {
    document.querySelectorAll('.skill-card').forEach(card => {
        card.style.opacity = '0';
        setTimeout(() => {
            card.style.transition = 'opacity 1s';
            card.style.opacity = '1';
        }, 300);
    });
    
    // 添加页面加载完成的提示
    const loadingIndicator = document.createElement('div');
    loadingIndicator.className = 'loading-complete';
    loadingIndicator.textContent = '加载完成';
    document.body.appendChild(loadingIndicator);
    
    setTimeout(() => {
        loadingIndicator.style.opacity = '0';
        setTimeout(() => {
            loadingIndicator.remove();
        }, 1000);
    }, 2000);
});

// 计算器功能实现
class Calculator {
    constructor(displayElement) {
        this.displayElement = displayElement;
        this.displayValue = '0';
        this.firstOperand = null;
        this.operator = null;
        this.waitingForSecondOperand = false;
        this.displayFormula = '';  // 添加用于显示公式的属性
    }

    updateDisplay() {
        // 显示当前输入值和运算符
        this.displayElement.textContent = this.displayFormula || this.displayValue;
    }

    inputDigit(digit) {
        if (this.waitingForSecondOperand) {
            this.displayValue = digit;
            this.displayFormula += digit;  // 添加数字到公式显示
            this.waitingForSecondOperand = false;
        } else {
            this.displayValue = this.displayValue === '0' ? digit : this.displayValue + digit;
            this.displayFormula = this.displayFormula === '0' ? digit : this.displayFormula + digit;
        }
    }

    inputDecimal() {
        if (this.waitingForSecondOperand) {
            this.displayValue = '0.';
            this.displayFormula += '0.';
            this.waitingForSecondOperand = false;
            return;
        }
        if (!this.displayValue.includes('.')) {
            this.displayValue += '.';
            this.displayFormula += '.';
        }
    }

    handleOperator(nextOperator) {
        const inputValue = parseFloat(this.displayValue);

        if (this.operator && this.waitingForSecondOperand) {
            // 更新运算符显示
            this.operator = nextOperator;
            this.displayFormula = this.displayFormula.slice(0, -1) + (nextOperator || '');
            return;
        }

        if (this.firstOperand === null && !isNaN(inputValue)) {
            this.firstOperand = inputValue;
        } else if (this.operator) {
            const result = this.calculate(this.firstOperand, inputValue, this.operator);
            this.displayValue = `${parseFloat(result.toFixed(7))}`;
            this.firstOperand = result;
            
            // 如果按下等号，显示完整计算结果
            if (nextOperator === null) {
                this.displayFormula = this.displayValue;
            } else {
                this.displayFormula = this.displayValue + nextOperator;
            }
        }

        this.waitingForSecondOperand = true;
        this.operator = nextOperator;
        
        // 添加运算符到公式显示
        if (nextOperator && !this.displayFormula.endsWith(nextOperator)) {
            this.displayFormula = this.displayValue + nextOperator;
        }
    }

    calculate(firstOperand, secondOperand, operator) {
        switch (operator) {
            case '+': return firstOperand + secondOperand;
            case '-': return firstOperand - secondOperand;
            case '*': return firstOperand * secondOperand;
            case '/': return firstOperand / secondOperand;
            case '%': return firstOperand % secondOperand;
            default: return secondOperand;
        }
    }

    clear() {
        this.displayValue = '0';
        this.firstOperand = null;
        this.operator = null;
        this.waitingForSecondOperand = false;
        this.displayFormula = '0';  // 重置公式显示
    }

    delete() {
        if (this.displayValue.length > 1) {
            this.displayValue = this.displayValue.slice(0, -1);
            this.displayFormula = this.displayFormula.slice(0, -1);
        } else {
            this.displayValue = '0';
            this.displayFormula = '0';
        }
    }
}

// 初始化计算器
window.addEventListener('load', function() {
    const calculator = new Calculator(document.getElementById('display'));
    
    document.querySelector('.calculator-keys').addEventListener('click', event => {
        const { target } = event;
        if (!target.matches('button')) return;

        if (target.classList.contains('operator')) {
            const action = target.dataset.action;
            if (action === 'clear') {
                calculator.clear();
            } else if (action === 'delete') {
                calculator.delete();
            } else if (action === '=') {
                calculator.handleOperator(null);
            } else {
                calculator.handleOperator(action);
            }
        } else if (target.textContent === '.') {
            calculator.inputDecimal();
        } else {
            calculator.inputDigit(target.textContent);
        }

        calculator.updateDisplay();
    });
}); 